package com.yenlien.traceyun.plant.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.interceptor.DataAuth;
import com.yenlien.traceyun.business.utils.EasyPoiUtils;
import com.yenlien.traceyun.plant.bean.CostDTO;
import com.yenlien.traceyun.plant.dao.InputsDao;
import com.yenlien.traceyun.plant.entity.Inputs;
import com.yenlien.traceyun.plant.service.InputsService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 投入品
 *
 * @author maybe
 * @date 2020年7月27日
 */
@RestController
@RequestMapping("/inputs")
public class InputsController extends BaseController {
    @Autowired
    private InputsService inputsService;

    @Autowired
    private InputsDao inputsDao;

    //列表查询
    @DataAuth
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody JSONObject params) {
        Inputs inputs = new Inputs();
        inputs.setPage(params.getIntValue("page"));
        inputs.setPageSize(10);
        inputs.setInputsName(params.getString("inputsName"));
        inputs.setType(params.getString("type"));
        return buildPage(inputsService.findByList(inputs));
    }

    @GetMapping(value = "/getCost")
    public Object getCost(String startTime, String endTime, String companyId) {
        return success(inputsService.getCost(startTime, endTime, companyId));
    }

    @GetMapping(value = "/getCostTable")
    public Object getCost2(String startTime, String endTime, String companyId, String type) {
        return success(inputsService.getCostTable(startTime, endTime, companyId, type));
    }

    @GetMapping(value = "/exportCostTable")
    public void exportCostTable(String startTime, String endTime, String companyId, String type, HttpServletResponse response) {
        try {
            EasyPoiUtils.exportExcel(inputsService.exportCostTable(startTime, endTime, companyId, type), "成本数据", "成本数据", CostDTO.class, "成本数据", response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    //根据ID查询详情
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(inputsService.findById(id));
    }

    //新增
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Inputs inputs, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        inputsService.save(inputs, files);
        return success("success");
    }

    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    public Object save2(@RequestBody Inputs inputs) throws Exception {
        inputsService.save2(inputs);
        return success("success");
    }

    @GetMapping(value = "/dele/{id}")
    public Object dele(@PathVariable("id") String id) {
        inputsDao.ljDelete(id);
        return success("ok");
    }

    //查询下拉
    @DataAuth
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() {
        return success(inputsService.findAll());
    }

    /**
     * 实时动态
     *
     * @return
     */
    @PostMapping("/dynamic")
    public Object dynamic() {
        return buildPage(inputsService.dynamic());
    }

    @PostMapping("/dynamic2")
    public Object dynamic2(String companyId) {
        return inputsService.dynamic2(companyId);
    }

}
